<?php
namespace Crm\Controller;

use Zend\View\Model\ViewModel;
class BannerController extends AbstractController
{
    
    public function indexAction(){
        $bannerTable=$this->getBannerTable();
        $typeTable=$this->gettypeTable();
        $indexBanner = array();
        $indexBanner=$bannerTable->getBannerList(array('type'=>1));
        $materialBanner = array();
        $materialBanner=$bannerTable->getBannerList(array('type'=>2));
        $indexTyper = array();
        $indexType=$typeTable->getTypeList();
        return new ViewModel(array(
            'indexBanner'=> $indexBanner,
            'materialBanner'=> $materialBanner,
            'indexType' => $indexType,
        ));
        
    }
    
    public function saveAction(){
        $data = $this->request->getPost()->toArray();
        $bannerTable=$this->getBannerTable();
        $typeTable=$this->gettypeTable();
        $bannerTable->beginTransaction();
        
        try{
            //首页banner
            for($i=1;$i<9;$i++){
                $banId    = 'banId_'.$i;
                $banTitle = 'banTitle_'.$i;
                $banUrl   = 'banUrl_'.$i;
                $banFile  = 'banFile_'.$i;
                if(isset($data[$banId])){
                    $banItem=array(
                        'type'=>1,
                        'title'=>$data[$banTitle],
                        'url'=>$data[$banUrl],
                    );
                    //图片上传
                    if($_FILES[$banFile]['error']==0){
                        $fileInfo = $this->uploadImg($banFile);
                        if ($fileInfo['state']==='SUCCESS') {
                            $banItem['image'] = $fileInfo['url'];
                        }else{
                            $bannerTable->rollBack();
                            $this->flashMessenger()->addErrorMessage("图片保存失败！");
                            return $this->redirect()->toRoute('crm/banner');
                        }
                    }
                    
                    if($data[$banId] == '' && $_FILES[$banFile]['error']==0){
                        $bannerTable->addItem($banItem);
                    }elseif($data[$banId] != '' && $_FILES[$banFile]['error']!=0 && $data[$banTitle] == "" && $data[$banUrl] ==""){
                        $bannerTable->deleteItem($data[$banId]);
                    }elseif($data[$banId] != ''){
                        $bannerTable->updateItem($data[$banId],$banItem);
                    }
                    
                }
            }
            
            //首页类目
            for($i=1;$i<6;$i++){
                $typeId    = 'typeId_'.$i;
                $typeName = 'typeName_'.$i;
                $typeLink   = 'typeLink_'.$i;
                $typeFile  = 'typeFile_'.$i;
                $typeItem=array(
                    'type_name'=>$data[$typeName],
                    'type_Link'=>$data[$typeLink],
                    'sequence'=>$i,
                );
                
                //图片上传
                if($_FILES[$typeFile]['error']==0){
                    $fileInfo = $this->uploadImg($typeFile);
                    if ($fileInfo['state']==='SUCCESS') {
                        $typeItem['type_thumb_image'] = $fileInfo['url'];
                    }else{
                        $bannerTable->rollBack();
                        $this->flashMessenger()->addErrorMessage("图片保存失败！");
                        return $this->redirect()->toRoute('crm/banner');
                    }
                }
                if($data[$typeId] == '' && $_FILES[$typeFile]['error']==0){
                    $typeTable->addItem($typeItem);
                }elseif($data[$typeId] !== ''){
                    //var_dump($typeItem);
                    $typeTable->updateItem($data[$typeId],$typeItem);
                }
            }
            //溯源banner
            for($i=1;$i<9;$i++){
                $matId    = 'matId_'.$i;
                $matTitle = 'matTitle_'.$i;
                $matUrl   = 'matUrl_'.$i;
                $matFile  = 'matFile_'.$i;
                if(isset($data[$matId])){
                    $matItem=array(
                        'type'=>2,
                        'title'=>$data[$matTitle],
                        'url'=>$data[$matUrl],
                    );
                    //图片上传
                    if($_FILES[$matFile]['error']==0){
                        $fileInfo = $this->uploadImg($matFile);
                        if ($fileInfo['state']==='SUCCESS') {
                            $matItem['image'] = $fileInfo['url'];
                        }else{
                            $bannerTable->rollBack();
                            $this->flashMessenger()->addErrorMessage("图片保存失败！");
                            return $this->redirect()->toRoute('crm/banner');
                        }
                    }
                    if($data[$matId] == '' && $_FILES[$matFile]['error']==0){
                        $bannerTable->addItem($matItem);
                    }elseif($data[$matId] != '' && $_FILES[$matFile]['error']!=0 && $data[$matTitle] == "" && $data[$matUrl] ==""){
                        $bannerTable->deleteItem($data[$matId]);
                    }elseif($data[$matId] != ''){
                        $bannerTable->updateItem($data[$matId],$matItem);
                    }
                }
            }
            $bannerTable->commit();
            $this->flashMessenger()->addSuccessMessage("保存成功！");
            return $this->redirect()->toRoute('crm/banner');
        } 
        catch(\Exception $e)
        {
            $bannerTable->rollBack();
//            die($e);
            $this->flashMessenger()->addErrorMessage("保存失败！");
            return $this->redirect()->toRoute('crm/banner');
        }
    }
    
}